﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using System.IO;
using System.Text.RegularExpressions;
using CDStore3Tiers;

namespace CDStore3Tiers
{
    public class XuLyForm
    {
        public string CToString(object obj)
        {
            if (obj == null)
            {
                return "";
            }
            else
                return obj.ToString().Trim();
        }
        public string CToInt(object obj)
        {

            if (obj == null)
                return (null);
            else
                return obj.ToString().Trim();
        }
        public static void MaxSize(Form frm)
        {
            //frm.MaximumSize = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
        }
        public void loadDGV(BindingNavigator bn, DataGridView dgv, Object ob)
        {
            BindingSource bds = new BindingSource();
            bds.DataSource = ob;
            bn.BindingSource = bds;
            dgv.DataSource = bds;
        }
        public void save(DataGridView dgv, StoreCDDataContext cn, ContainerControl ctl)
        {
            StoreCDDataContext db = new StoreCDDataContext();
            dgv.EndEdit();
            ctl.Validate();
            cn.SubmitChanges();
            cn = new StoreCDDataContext();
            cn.Connection.ConnectionString = getConfig.getConnection();
        }
        public void deleteDGV(DataGridView dgv)
        {
            foreach (DataGridViewRow r in dgv.SelectedRows)
            {
                dgv.Rows.Remove(r);
            }

        }
        //combobox DGV
        public void loadComboboxDGV(DataGridViewComboBoxColumn col, Object ob, string display, string value)
        {
            col.DataSource = ob;
            col.DisplayMember = display;
            col.ValueMember = value;
        }
        public void loadComboboxDGVDot(DataGridViewComboBoxExColumn col, Object ob, string display, string value)
        {
            col.DataSource = ob;
            col.DisplayMember = display;
            col.ValueMember = value;
        }
        //combobox
        public void loadCombobox(ComboBoxEx col, Object ob, string display, string value)
        {
            col.DataSource = ob;
            col.DisplayMember = display;
            col.ValueMember = value;
        }
        //combobox toolstrip
        public void loadComboboxToolstrip(ToolStripComboBox col, Object ob, string display, string value)
        {
            col.ComboBox.DataSource = ob;
            col.ComboBox.DisplayMember = display;
            col.ComboBox.ValueMember = value;
        }
        public void validate(string str, string pattern, string mes)
        {
            if (string.IsNullOrEmpty(str)) throw new Exception("Chưa nhập giá trị !");
            if (!Regex.IsMatch(str, pattern))
                throw new Exception(mes);
        }
        public bool isValidate(string str, string pattern)
        {
            if (string.IsNullOrEmpty(str)) throw new Exception("Chưa nhập giá trị !");
            if (Regex.IsMatch(str, pattern))
                return true;
            else
                return false;
        }
        public bool isValidateWithMessage(string str, string pattern, string mes)
        {
            if (string.IsNullOrEmpty(str))
                DevComponents.DotNetBar.MessageBoxEx.Show("Chưa nhập giá trị !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            bool val = true;
            if (!Regex.IsMatch(str, pattern))
            {
                val = false;
                DevComponents.DotNetBar.MessageBoxEx.Show(mes, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return val;
        }

        public void ghiFileConfig(string serVer, string db)
        {
            FileStream file = new FileStream(System.IO.Directory.GetCurrentDirectory().ToString() + "\\conFiguration.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter wr = new StreamWriter(file);
            wr.WriteLine(@"Data Source=" + serVer + ";Initial Catalog=" + db + ";Integrated Security=True");

            wr.Close();
            file.Close();
        }
        
        public void loadTreeviewBaihat(TreeView tr, List<BaiHat> list)
        {
            TreeNode node;
            tr.Nodes.Clear();
            if (list.Count() > 0)
            {
                foreach (BaiHat cd in list)
                {
                    node = new TreeNode(cd.TenBH+"-----"+ cd.CaSi);
                    node.Tag = cd.MaBH;
                    tr.Nodes.Add(node);
                }
            }
        }
    }
}
